package com.xinzhidi.web.onlineMonitor.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.google.common.collect.Lists;
import com.xinzhidi.common.constant.AgreementConstants;
import com.xinzhidi.common.core.controller.BaseController;
import com.xinzhidi.common.core.domain.AjaxResult;
import com.xinzhidi.common.core.page.TableDataInfo;
import com.xinzhidi.web.equipment.service.EquipmentService;
import com.xinzhidi.web.onlineMonitor.domain.po.MeterReadPo;
import com.xinzhidi.web.onlineMonitor.domain.po.OnlineMonitorPo;
import com.xinzhidi.web.onlineMonitor.domain.vo.OnlineMonitorVo;
import com.xinzhidi.web.onlineMonitor.service.OnlineMonitorService;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 在线监测
 *
 * @author cx
 */
@RestController
@RequestMapping("/onlineMonitor")
@RequiredArgsConstructor
@Slf4j
public class OnlineMonitorController extends BaseController {

    private final OnlineMonitorService monitorService;

    private final EquipmentService equipmentService;

    @ApiOperation("在线监测-楼栋列表")
    @PostMapping("/listBuild")
    public TableDataInfo<OnlineMonitorVo> listBuild(@Validated @RequestBody OnlineMonitorPo po) {
        startPagePost(po);
        return monitorService.listBuild(po);
    }

    @ApiOperation("在线监测-单元列表")
    @PostMapping("/listBuildUnit")
    public TableDataInfo<OnlineMonitorVo> listBuildUnit(@Validated @RequestBody OnlineMonitorPo po) {
        startPagePost(po);
        return monitorService.listBuildUnit(po);
    }

    @ApiOperation("在线监测-住户列表")
    @PostMapping("/listHousehold")
    public TableDataInfo<OnlineMonitorVo> listHousehold(@Validated @RequestBody OnlineMonitorPo po) {
        startPagePost(po);
        return monitorService.listHousehold(po);
    }

    @ApiOperation("抄表")
    @PostMapping("/meteRead")
    public AjaxResult MeterRead(@RequestBody List<MeterReadPo> list) throws InterruptedException {
        List<MeterReadPo> newList = Lists.newArrayList();
        if(CollectionUtils.isNotEmpty(list)){
            List<MeterReadPo> poList;
            for(MeterReadPo po : list){
                poList = monitorService.getReadPo(po , "1");
                if(CollectionUtils.isNotEmpty(poList)){
                    newList.addAll(poList);
                }
            }
        }
        monitorService.meterRead(AgreementConstants.InstructBusinessType.MANUAL_METER_READ, newList);
        return AjaxResult.success();
    }

    @ApiOperation("一键抄表")
    @PostMapping("/oneClickMeter")
    public AjaxResult oneClickMeter(@RequestBody MeterReadPo po) throws InterruptedException {
        monitorService.oneClickMeter(po);
        return AjaxResult.success();
    }


}
